package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.MarketAdminService;
import com.cskaoyan.market.util.Md5Utils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MarketAdminServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/3/17 14:44
 * @Version V1.0
 **/
public class MarketAdminServiceImpl implements MarketAdminService {
    @Override
    public MarketAdmin login(String username, String password) {
        // 调用dao代码来进行查询
        // SqlSession一定不能写成成员变量
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample adminExample = new MarketAdminExample();
        String encryptPassword = Md5Utils.getMd5(password, username);
        adminExample.or().andUsernameEqualTo(username).andPasswordEqualTo(encryptPassword);
        MarketAdmin admin = adminMapper.selectOneByExample(adminExample);
        sqlSession.commit();
        // 一定要记得及时close，否则会出现一些问题
        sqlSession.close();
        return admin;
    }

    @Override
    public void updateById(MarketAdmin admin) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = session.getMapper(MarketAdminMapper.class);
        adminMapper.updateByPrimaryKeySelective(admin);
        // 记得提交事务
        session.commit();
        session.close();
    }

    @Override
    public List<String> getPerms(String username) {
        List<String> list = new ArrayList<>();
        if("mall123".equals ( username )){
            list.add("POST /admin/category/delete");
            list.add("GET /admin/collect/list");
            list.add("GET /admin/category/read");
            list.add("GET /admin/footprint/list");
            list.add("GET /admin/history/list");
            list.add("POST /admin/brand/delete");
            list.add("GET /admin/category/list");
            list.add("POST /admin/category/update");
            list.add("GET /admin/user/list");
            list.add("GET /admin/brand/list");
            list.add("GET /admin/address/list");
            list.add("POST /admin/category/create");
            list.add("POST /admin/brand/create");
            list.add("GET /admin/feedback/list");
            list.add("GET /admin/brand/read");
            list.add("POST /admin/brand/update");
        }else if("promotion123".equals ( username )){
            list.add("POST /admin/topic/create");
            list.add("POST /admin/ad/create");
            list.add("GET /admin/topic/read");
            list.add("POST /admin/coupon/delete");
            list.add("GET /admin/ad/read");
            list.add("GET /admin/coupon/list");
            list.add("POST /admin/groupon/delete");
            list.add("GET /admin/groupon/list");
            list.add("POST /admin/topic/delete");
            list.add("POST /admin/ad/delete");
            list.add("POST /admin/groupon/update");
            list.add("GET /admin/groupon/listRecord");
            list.add("POST /admin/coupon/create");
            list.add("GET /admin/topic/list");
            list.add("POST /admin/ad/update");
            list.add("GET /admin/ad/list");
            list.add("GET /admin/coupon/read");
            list.add("POST /admin/topic/update");
            list.add("POST /admin/groupon/create");
            list.add("POST /admin/coupon/update");
        }
        return list;
    }

}
